<?php
namespace backend\modules\statistics\controllers;

use backend\models\activeRecords\TUser;
use common\libs\Constants;
use common\models\User;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use backend\models\activeRecords\StatisticsDay;
use backend\modules\statistics\models\StatisticsDaySearch;
use yii\web\Response;
use yii\widgets\ActiveForm;

/**
 * StatisticsDayController implements the CRUD actions for StatisticsDay model.
 */
class StatisticsDayController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all StatisticsDay models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new StatisticsDaySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);
    }

    /**
     * Displays a single StatisticsDay model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $params = Yii::$app->request->bodyParams;
        if(empty($params)){
            return $this->renderAjax('view', [
            'model' => $model,
            ]);
        }else{
            if($model->load($params) && $model->save()){
                return $this->redirect('index');
            }else{
                return $this->renderAjax('view', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Creates a new StatisticsDay model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new StatisticsDay();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect('index');
        } else {
            return $this->renderAjax('create', ['model' => $model]);
        }
    }

    /**
     * Updates an existing StatisticsDay model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect('index');
        } else {
            return $this->renderAjax('update', ['model' => $model]);
        }
    }

    /**
     * Deletes an existing StatisticsDay model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the StatisticsDay model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return StatisticsDay the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = StatisticsDay::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


    /**
    * Asynchronous Validate Form Model
    */
    public function actionValidateForm ()
    {
        $model = new StatisticsDay();
        $model->load(Yii::$app->request->post());
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model);
    }

    public function actionBindingUser()
    {
        $userList = User::findAll(['parent'=>Yii::$app->user->getIdentity()->parent]);
        $model = new TUser();
        $model->setScenario('binding');
        if($model->load(Yii::$app->request->bodyParams)){
            $tuserInfo = $model->findOne(['username'=>$model->username]);
            $tuserInfo->spread_key = Yii::$app->user->getIdentity()->parent;
            $tuserInfo->spread_key_1 = Constants::ANCHOR;
            $tuserInfo->spread_key_2 = $model->spread_key_2;
            $tuserInfo->reg_game_id = $model->reg_game_id;
            $tuserInfo->reg_server_id = $model->reg_server_id;
            $tuserInfo->reg_game_name = $model->game->game_name;
            $tuserInfo->reg_server_name = $model->server->server_name;
            if($tuserInfo->save()){
                return $this->redirect('index');
            }
        }
        return $this->render('binding-user',['model'=>$model,'userList'=>$userList]);
    }


}
